library(stargazer)
library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

#----------------------------------------------------------------------------------------------------#
# ANES MODELS  -- 92-94
#----------------------------------------------------------------------------------------------------#
dat1 <- na.omit(anes9297[c("white_int92", "white_int94",
                            "pid92_7scaled", "pid94_7scaled", 
                            "rr92_scaled", "rr94_scaled", "V940005",
                            "anti_immig92", "soc_con92")])

### PID on RR
m1 <- lm(rr94_scaled ~  pid92_7scaled  + rr92_scaled +
           anti_immig92 + soc_con92, 
         data = dat1, weights = V940005, 
         subset = white_int92 == white_int94)
summary(m1)

### RR on PID
m10 <- lm(pid94_7scaled ~  pid92_7scaled  + rr92_scaled +
            anti_immig92 + soc_con92, 
          data = dat1, weights = V940005, 
          subset = white_int92 == white_int94)
summary(m10)

#----------------------------------------------------------------------------------------------------#
# CCAP 2008
#----------------------------------------------------------------------------------------------------#
dat2 <- na.omit(ccap08[c("pid7r_m_sc", "rr_m_scaled", "rr_o_scaled", 
                          "pid7r_o_sc", "weight",
                          "immig_m", "soc_iss")]) 

### PID on RR
m2 <- lm(rr_o_scaled ~ pid7r_m_sc  + rr_m_scaled +
           immig_m + soc_iss, 
         data = dat2, weights = weight)
summary(m2)

### RR on PID
m20 <- lm(pid7r_o_sc ~ pid7r_m_sc  + rr_m_scaled +
            immig_m + soc_iss, 
          data = dat2, weights = weight)
summary(m20)

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: March
#----------------------------------------------------------------------------------------------------#
ccap.p2 <- subset(ccap12, p2 == 1, 
                  select=c("rr_p1_scaled", "rr_p2_scaled", 
                           "pp_pid7r_sc", "pid7_sc", "weight",
                           "opp_bizreg", "soc_con", "anti_imm"))
dat3 <- na.omit(ccap.p2)
# PID on RR
m3 <- lm(rr_p2_scaled ~  pp_pid7r_sc  + rr_p1_scaled +
           anti_imm + soc_con + opp_bizreg, 
         data = dat3, weights = weight)
summary(m3)

# RR on PID
m30 <- lm(pid7_sc ~  pp_pid7r_sc  + rr_p1_scaled +
            anti_imm + soc_con + opp_bizreg, 
          data = dat3, weights = weight)
summary(m30)

#----------------------------------------------------------------------------------------------------#
# CCAP 2012: August
#----------------------------------------------------------------------------------------------------#
ccap.p3 <- subset(ccap12, p3 == 1, 
                  select=c("rr_p1_scaled", "rr_p3_scaled", 
                           "pp_pid7r_sc", "pid7_sc", "weight",
                           "opp_bizreg", "soc_con", "anti_imm"))
dat4 <- na.omit(ccap.p3)

# PID on RR
m4 <- lm(rr_p3_scaled ~  pp_pid7r_sc  + rr_p1_scaled + 
           anti_imm + soc_con + opp_bizreg, 
         data = dat4, weights = weight)
summary(m4)


# RR on PID
m40 <- lm(pid7_sc ~  pp_pid7r_sc  + rr_p1_scaled +
            anti_imm + soc_con + opp_bizreg, 
          data = dat4, weights = weight)
summary(m40)

#----------------------------------------------------------------------------------------------------#
# VSG 2012-2016
#----------------------------------------------------------------------------------------------------#
dat5 <- na.omit(vsg.dat[c("rr_sc_12", "rr_sc_16", "pid7_sc_12", 
                           "pid7_sc_16", "weight",
                           "soc_iss_12", "biz_reg_12", "anti_immig_12")])

# PID on RR
m5 <- lm(rr_sc_16 ~ pid7_sc_12 + rr_sc_12 +
           anti_immig_12 + soc_iss_12 + biz_reg_12, 
         data = dat5, weights = weight)
summary(m5)

# RR on PID
m50 <- lm(pid7_sc_16 ~ pid7_sc_12 + rr_sc_12 +
            anti_immig_12 + soc_iss_12 + biz_reg_12,
          data = dat5, weights = weight)
summary(m50)

#----------------------------------------------------------------------------------------------------#
# CCAP 2016
#----------------------------------------------------------------------------------------------------#
dat6 <- na.omit(ccap16[c("b_rr_sc", "p_rr_sc", "b_pid7_sc", "p_pid7_sc", "weight_post",
                          "b_anti_immig", "b_soc_con")])

# PID on RR
m6 <- lm(p_rr_sc ~ b_pid7_sc + b_rr_sc +
           b_anti_immig + b_soc_con, 
         data = dat6, weights = weight_post)
summary(m6)

# RR on PID
m60 <- lm(p_pid7_sc ~ b_pid7_sc + b_rr_sc +
            b_anti_immig + b_soc_con, 
          data = dat6, weights = weight_post)
summary(m60)

#----------------------------------------------------------------------------------------------------#
# Table Generation
#----------------------------------------------------------------------------------------------------#
m1_p <- m1
m10_p <- m10
m2_p <- m2
m20_p <- m20
m3_p <- m3
m30_p <- m30
m4_p <- m4
m40_p <- m40
m5_p <- m5
m50_p <- m50
m6_p <- m6
m60_p <- m60

names(m1_p$coefficients) <- names(coef(m3_p))[-max(length(names(coef(m3_p))))]
names(m10_p$coefficients) <-names(coef(m3_p))[-max(length(names(coef(m3_p))))]
names(m2_p$coefficients) <- names(coef(m3_p))[-max(length(names(coef(m3_p))))]
names(m20_p$coefficients) <- names(coef(m3_p))[-max(length(names(coef(m3_p))))]
names(m3_p$coefficients) <- names(coef(m3_p))
names(m30_p$coefficients) <- names(coef(m3_p))
names(m4_p$coefficients) <- names(coef(m3_p))
names(m40_p$coefficients) <- names(coef(m3_p))
names(m5_p$coefficients) <- names(coef(m3_p))
names(m50_p$coefficients) <- names(coef(m3_p))
names(m6_p$coefficients) <- names(coef(m3_p))[-max(length(names(coef(m3_p))))]
names(m60_p$coefficients) <- names(coef(m3_p))[-max(length(names(coef(m3_p))))]


### Table Generation
stargazer(m1_p, m10_p, m2_p, m20_p, m3_p, m30_p, m4_p, m40_p, m5_p, m50_p, m6_p, m60_p,
          title = "Relationship between Whites' Partisanship and Racial Resentment, with additional attitudes",
          covariate.labels = c("Partisanship$_{t-1}$", "Racial Resentment$_{t-1}$",
                               "Anti-Immigration Attitudes$_{t-1}$",
                               "Culture War Attitudes$_{t-1}$",
                               "Economic Orientations$_{t-1}$"),
          model.numbers = F, dep.var.caption = "",
          dep.var.labels = c("Racial Resentment$_t$", "Partisanship$_t$", "Racial Resentment$_t$", "Partisanship$_t$", "Racial Resentment$_t$", "Partisanship$_t$", "Racial Resentment$_t$", "Partisanship$_t$",
                             "Racial Resentment$_t$", "Partisanship$_t$", "Racial Resentment$_t$", "Partisanship$_t$"), 
          no.space = T,
          notes = c("OLS regression results with standard errors in parentheses. Analyses employ population weights. Variables scaled 0-1."), 
          notes.align = "l", intercept.bottom = T, 
          label = c("mods_with_covs"),
          digits = 3, omit.stat = c("f", "adj.rsq"), 
          df = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05)
)